//
//  AXFHomeCollectionViewCell.m
//  AIXF
//
//  Created by Macx on 2017/7/10.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFHomeCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "AXFAccountManager.h"


@interface AXFHomeCollectionViewCell ()
//商品图片
@property (weak, nonatomic) IBOutlet UIImageView *bigImageView;
//商品名字
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
//重量
@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;
//价格
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
//超市价格
@property (weak, nonatomic) IBOutlet UILabel *marketPriceLabel;
//增加按钮
@property (weak, nonatomic) IBOutlet UIButton *addButton;
//减少按钮
@property (weak, nonatomic) IBOutlet UIButton *deleteButton;
//买一赠一
@property (weak, nonatomic) IBOutlet UILabel *pmdescLabel;
//购买数量
@property (weak, nonatomic) IBOutlet UILabel *orderNumLabel;

@property (weak, nonatomic) IBOutlet UIImageView *buyOneImageView;

@end
@implementation AXFHomeCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
}

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI
{
    
}

- (void)setModel:(AXFHomeFirstSellModel *)model
{
    _model = model;
  
    self.contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    
    self.nameLabel.text = model.name;
    
    self.specificsLabel.text = model.specifics;
    
    self.priceLabel.text = [NSString stringWithFormat:@"%@元",model.price];

    self.marketPriceLabel.text = [NSString stringWithFormat:@"%@元",model.market_price];
    
    self.pmdescLabel.text = model.pm_desc;
    
    self.orderNumLabel.hidden = !model.count;
    
    self.deleteButton.hidden = !model.count;
    
    self.orderNumLabel.text = [NSString stringWithFormat:@"%d", model.count];
    
    self.buyOneImageView.hidden = !model.pm_desc.length;
    


    
}
- (IBAction)reduceAction:(UIButton *)sender
{
    self.orderNum--;
    
    //_AXFAccountManager.goodsNum--;
    
    _model.count = self.orderNum;
    
    self.clickIncrement = YES;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kDecrementNotification object:_model];
    
    NSLog(@"%lu",(unsigned long)_AXFAccountManager.orderArray.count);
    

}
- (IBAction)increseAction:(id)sender
{
    
    if (self.orderNum == self.model.number)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"postNum" object:nil];
        return;
    }
    
    self.orderNum++;
    
    //_AXFAccountManager.goodsNum++;
    
    _model.count = self.orderNum;
    
    self.clickIncrement = NO;
    
    CGPoint center = self.bigImageView.center;
    
    CGPoint convertPoint = [self.bigImageView convertPoint:center toView:kWindow];
    
  
     [[NSNotificationCenter defaultCenter] postNotificationName:kIncrementNotification object:nil userInfo:@{@"point":[NSValue valueWithCGPoint:convertPoint], @"model":_model}];
    
    //NSLog(@"%lu",(unsigned long)_AXFAccountManager.orderArray.count);
    
//    if (![_AXFAccountManager.orderArray containsObject:self.model]) {
//        // 如果数组中没有添加过这个对象就进行添加
//        [_AXFAccountManager.orderArray addObject:self.model];
//    }
    
    
}

- (void)setOrderNum:(int)orderNum
{
    _orderNum = orderNum;
    
    self.orderNumLabel.text = [NSString stringWithFormat:@"%d", orderNum];
    
    /**
     hidden = YES; 非0的时候是隐藏的
     */
    self.deleteButton.hidden = !orderNum;
    
    self.orderNumLabel.hidden = !orderNum;
    
    _model.count = orderNum;
    
}

@end
